/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.plechinger.mcremote.client;

import com.plechinger.mcremote.security.SecurityUtil;
import java.io.*;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Lukas
 */
public class ClientSettings {

    private Properties properties;
    private static ClientSettings settings = null;
    private File propertiesFile;

    private ClientSettings() {
        propertiesFile = new File(System.getProperty("user.home")
                + System.getProperty("file.separator") + ".mcremote"
                + System.getProperty("file.separator")
                + "mcremote_client.properties");
        properties = new Properties();
        try {
            if (!propertiesFile.exists()) {
                File pa = propertiesFile.getParentFile();
                pa.mkdirs();
                propertiesFile.createNewFile();
                //Datei verstecken
                try {
                    Runtime.getRuntime().exec("attrib +h " + pa.getPath());
                } catch (Exception e) {
                    //Wird geworfen, wenn keine Windows installation
                }
            }
            properties.load(new FileInputStream(propertiesFile));
        } catch (FileNotFoundException ex) {
        } catch (IOException ex) {
        }


    }

    public static ClientSettings getSettings() {
        if (settings == null) {
            settings = new ClientSettings();
        }
        return settings;
    }

    public void store() {
        try {
            properties.store(new FileOutputStream(propertiesFile), "MCRemote Settings\nDo not customize if you not know what you are doing!");
        } catch (FileNotFoundException ex) {
        } catch (IOException ex) {
        }
    }

    public String getHost() {
        return properties.getProperty("server.host", "localhost");
    }

    public ClientSettings setHost(String host) {
        properties.setProperty("server.host", host);
        return this;
    }

    public int getPort() {
        return Integer.parseInt(properties.getProperty("server.host", "29900"));
    }

    public ClientSettings setPassword(String password) {
        byte[] pw = password.getBytes();
        String pass = "";
        for (int i = 0; i < pw.length; i++) {
            byte b = pw[i];
            pass += String.format("%01x", b);
        }
        properties.setProperty("server.password", pass);
        return this;
    }

    public String getPassword() {
        String pass = properties.getProperty("server.password", "");
        if (pass != null && !"".equals(pass)) {
            String password = "";

            for (int i = 0; i < pass.length() / 2; i++) {
                String part = pass.substring(i * 2, i * 2 + 2);
                password += (char) Byte.parseByte(part, 16);
            }
            return password;
        }
        return "";
    }
    
    public Key getEncryptionKey() {
        try {
            return SecurityUtil.getKey(properties.getProperty("network.encryptionKey"));
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(ClientSettings.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidKeyException ex) {
            Logger.getLogger(ClientSettings.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(ClientSettings.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidKeySpecException ex) {
            Logger.getLogger(ClientSettings.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}
